package club.controller;

import club.pojo.AdoptAnimal;
import club.pojo.Pet;
import club.pojo.User;
import club.service.AdoptAnimalService;
import club.service.PetService;
import club.util.Message;
import com.github.pagehelper.PageInfo;
import club.pojo.AdoptAnimal;
import club.service.AdoptAnimalService;
import club.util.Message;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Date;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

@Controller
@RequestMapping("/adopt")
public class AdoptAnimalController {
    @Resource
    private AdoptAnimalService adoptAnimalService;
    @Resource
    private PetService petService;

    @RequestMapping("/apply")
    @ResponseBody
    public Message agree(String userName, @RequestParam("pn") Integer pageNum, Integer state) {
        Integer pageSize = 5;
        if (pageNum == 0) {
            pageNum = 1;
        }
        PageInfo<AdoptAnimal> pageInfo = adoptAnimalService.allAdoptAnimal(userName, pageNum, pageSize, state);
        return Message.success().add("pageInfo", pageInfo);
    }

    @RequestMapping("/create")
    @ResponseBody
    public Message create(HttpSession session) {
        User user = (User) session.getAttribute("user");
        Pet pet = (Pet) session.getAttribute("pet");

        Pet pet1 = petService.findById(pet.getId());
        AdoptAnimal animal = adoptAnimalService.getByUserAndPet(user.getId(), pet.getId());
        if (null != animal) {
            Message result = Message.fail();
            String state;
            if (animal.getState().equals(1)) {
                state = "正在审核中";
                result.setMessage("您已经对" + pet1.getPetName() + "申请领养了，目前" + state + "。无需再次申请");
                return result;
            } else if (animal.getState().equals(2)) {
                state = "已经审核通过";
                result.setMessage("您已经对" + pet1.getPetName() + "申请领养了，目前" + state + "。无需再次申请");
                return result;
            }
        }
        if (null != pet1 && pet1.getState().equals(2)) {
            Message result = Message.fail();
            result.setMessage("对不起，您来晚了，" + pet1.getPetName() + "已经被其他好心人士领养了。");
            return result;
        }

        AdoptAnimal adoptAnimal = new AdoptAnimal();
        adoptAnimal.setUserId(user.getId());
        adoptAnimal.setPetId(pet.getId());
        adoptAnimal.setAdoptTime(new Date());
        adoptAnimal.setState(1);
        adoptAnimalService.create(adoptAnimal);

        pet1.setState(1);
        petService.update(pet1);

        return Message.success();
    }

    @RequestMapping("/adopts")
    @ResponseBody
    public Message adopts(@RequestParam(required = false) String adoptTime, @RequestParam(required = false, value = "pn") Integer pageNum) {
        int pageSize = 3;
        if (pageNum == null) {
            pageNum = 1;
        }


        PageInfo<AdoptAnimal> pageInfo = adoptAnimalService.all(adoptTime, pageNum, pageSize);
        return Message.success().add("pageInfo", pageInfo);
    }

    @RequestMapping("/disAgree")
    @ResponseBody
    public Message disAgree(Integer id) {
        int update = adoptAnimalService.update(id, 0);
        if (update > 0) {
            return Message.success();
        }
        return Message.fail();
    }

    @RequestMapping("/agree")
    @ResponseBody
    public Message agree(Integer id) {
        System.out.println(id);
        int update = adoptAnimalService.update(id, 2);
        if (update > 0) {
            return Message.success();
        }
        return Message.fail();
    }
}
